#include "DlgCash.h"
#include <wx/sizer.h>
#include <wx/statbox.h>
#include <wx/stattext.h>
#include <wx/msgdlg.h>
//(*InternalHeaders(DlgUser)
#include <wx/intl.h>
#include <wx/string.h>
//*)

//(*IdInit(DlgUser)
//*)
const wxString CDlgCash::TABLENAME=_T("cash");
const wxString CDlgCash::VIEWNAME=_T("VcashFull");
#define MORNING        _T("上午")
#define AFTERNOON      _T("下午")
#define NIGHT          _T("晚上")
#define TIME_OTHER     _T("其他时间段")
const wxString WINDOWS_TEXT = _T("现金流管理模块");
//控件ID
const long CDlgCash::IDC_BTN_ADD_MOD= wxNewId();
const long CDlgCash::IDC_BTN_EXIT= wxNewId();
const long CDlgCash::IDC_BTN_SEARCH= wxNewId();
const long CDlgCash::IDC_BTN_DEL= wxNewId();
const long CDlgCash::IDC_COMBO_TYPE= wxNewId();
const long CDlgCash::IDC_COMBO_TIME= wxNewId();
const long CDlgCash::IDC_COMBO_SEARCH= wxNewId();
const long CDlgCash::IDC_EDIT_SEARCH= wxNewId();
const long CDlgCash::IDC_EDIT_BALANCE= wxNewId();
const long CDlgCash::IDC_EDIT_USER= wxNewId();
const long CDlgCash::IDC_RICHEDIT_REMARK= wxNewId();
const long CDlgCash::IDC_THEDAY= wxNewId();
const long CDlgCash::IDC_LIST= wxNewId();

BEGIN_EVENT_TABLE(CDlgCash,wxDialog)
    //(*EventTable(DlgUser)
    //*)
END_EVENT_TABLE()

CDlgCash::CDlgCash(wxWindow* parent,wxWindowID id,const wxPoint& pos,const wxSize& size)
    :m_addormod_(-1)
{
    //(*Initialize(DlgUser)
    Create(parent, id, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE|wxMAXIMIZE_BOX|wxMINIMIZE_BOX, _T("id"));
    SetClientSize(wxDefaultSize);
    Move(wxDefaultPosition);
    //*)
    SetTitle(WINDOWS_TEXT);
    SetWindowStyle(GetWindowStyle()|wxRESIZE_BORDER|wxCLOSE_BOX|wxMAXIMIZE_BOX|wxMINIMIZE_BOX);
    CreateCtrlAndEvent();
    EnableAllCtrl(false);
    InitComboBoxAndListCtrl();
}

CDlgCash::~CDlgCash()
{
    //(*Destroy(DlgUser)
    //*)
}

void CDlgCash::CreateCtrlAndEvent()
{
    /////////创建控件并布局
    topSizer_=new wxBoxSizer(wxVERTICAL);//顶层窗口布局,从上到下包含每个了窗口布局
    wxBoxSizer *firSize=new wxBoxSizer(wxHORIZONTAL);
    wxBoxSizer *secSize=new wxBoxSizer(wxHORIZONTAL);
    wxBoxSizer *thiSize=new wxBoxSizer(wxHORIZONTAL);
    wxStaticBox *staticBox=new wxStaticBox(this,wxID_ANY,wxT("注意：双击列表数据可进行修改"));
    wxStaticBoxSizer *forSize=new wxStaticBoxSizer(staticBox,wxHORIZONTAL);
    wxBoxSizer *bottomSize=new wxBoxSizer(wxHORIZONTAL);
    topSizer_->Add(firSize,0,wxEXPAND|wxALL,0);
    topSizer_->Add(secSize,0,wxEXPAND|wxALL,0);
    topSizer_->Add(thiSize,0,wxEXPAND|wxALL,0);
    topSizer_->Add(forSize,10,wxEXPAND|wxALL,0);
    topSizer_->Add(bottomSize,0,wxEXPAND|wxALL,0);

    combo_search_=new wxChoice(this,IDC_COMBO_SEARCH);
    combo_time_=new wxChoice(this,IDC_COMBO_TIME);
    combo_type_=new wxChoice(this,IDC_COMBO_TYPE);
    btn_Add_Mod_=new wxButton(this,IDC_BTN_ADD_MOD,_T("添加"));
    btn_Del_=new wxButton(this,IDC_BTN_DEL,_T("删除"));
    btn_Exit_=new wxButton(this,IDC_BTN_EXIT,_T("退出"));
    btn_Search_=new wxButton(this,IDC_BTN_SEARCH,_T("查询"));
    text_search_=new wxTextCtrl(this,IDC_EDIT_SEARCH,wxEmptyString);
    text_balance_=new wxNumberCtrl(this,IDC_EDIT_BALANCE,wxEmptyString);
    text_user_=new wxTextCtrl(this,IDC_EDIT_USER,wxEmptyString);
    text_remark_=new wxTextCtrl(this,IDC_RICHEDIT_REMARK,wxEmptyString,wxDefaultPosition,wxSize(120,50),
                                wxTE_PROCESS_ENTER|wxTE_MULTILINE|wxHSCROLL);
    time_=new wxDatePickerCtrl(this,IDC_THEDAY,wxDefaultDateTime,wxDefaultPosition,
                               wxDefaultSize,wxDP_DROPDOWN|wxDP_SHOWCENTURY);
    list_=new wxListCtrl(this,IDC_LIST,wxDefaultPosition,wxDefaultSize,wxLC_REPORT|wxLC_SINGLE_SEL);

    firSize->Add(new wxStaticText(this,wxID_ANY,_T("查询类别")),1, wxTILE|wxALL|wxALIGN_RIGHT,5);
    firSize->Add(combo_search_,1, wxEXPAND|wxALL|wxALIGN_CENTER,5);
    firSize->Add(text_search_,1,wxEXPAND|wxALL|wxALIGN_CENTER,5);
    firSize->Add(btn_Search_,1,wxTILE|wxALL|wxALIGN_LEFT,5);
    secSize->Add(new wxStaticText(this,wxID_ANY,_T("时间")),0,wxFIXED_MINSIZE|wxALL|wxALIGN_RIGHT,5);
    secSize->Add(time_,1,wxEXPAND|wxALL|wxALIGN_CENTER,5);
    secSize->AddStretchSpacer(1);
    secSize->Add(new wxStaticText(this,wxID_ANY,_T("时间段")),0,wxFIXED_MINSIZE|wxALL|wxALIGN_RIGHT,5);
    secSize->Add(combo_time_,1,wxEXPAND|wxALL|wxALIGN_CENTER,5);
    secSize->AddStretchSpacer(1);
    secSize->Add(new wxStaticText(this,wxID_ANY,_T("收支数")),0,wxFIXED_MINSIZE|wxALL|wxALIGN_RIGHT,5);
    secSize->Add(text_balance_,1,wxEXPAND|wxALL|wxALIGN_CENTER,5);
    thiSize->Add(new wxStaticText(this,wxID_ANY,_T("经手人")),0,wxFIXED_MINSIZE|wxALL|wxALIGN_RIGHT,5);
    thiSize->Add(text_user_,1,wxEXPAND|wxALL|wxALIGN_CENTER,5);
    thiSize->AddStretchSpacer(1);
    thiSize->Add(new wxStaticText(this,wxID_ANY,_T("收支类别")),0,wxFIXED_MINSIZE|wxALL|wxALIGN_RIGHT,5);
    thiSize->Add(combo_type_,1,wxEXPAND|wxALL|wxALIGN_CENTER,5);
    thiSize->AddStretchSpacer(1);
    thiSize->Add(new wxStaticText(this,wxID_ANY,_T("备注")),0,wxFIXED_MINSIZE|wxALL|wxALIGN_RIGHT,5);
    thiSize->Add(text_remark_,1,wxEXPAND|wxALL|wxALIGN_CENTER,5);
    forSize->Add(list_,1,wxEXPAND | wxALL,5);
    bottomSize->Add(btn_Add_Mod_,1,wxTILE|wxALL|wxALIGN_RIGHT,5);
    bottomSize->Add(btn_Del_,1,wxTILE|wxALL|wxALIGN_CENTER,5);
    bottomSize->Add(btn_Exit_,1,wxTILE|wxALL|wxALIGN_LEFT,5);

    SetSizer(topSizer_);//绑定对话框和布局控件
    SetAutoLayout(true);
    topSizer_->Fit(this);//调用对话框大小
    topSizer_->SetSizeHints(this);//设置对话框最小大小

    ///////创建事件
    Connect(IDC_BTN_ADD_MOD,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&CDlgCash::OnBnClickedBtnAddMod);
    Connect(IDC_BTN_EXIT,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&CDlgCash::OnBnClickedBtnExit);
    Connect(IDC_BTN_SEARCH,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&CDlgCash::OnBnClickedBtnSearch);
    Connect(IDC_BTN_DEL,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&CDlgCash::OnBnClickedBtnDel);
    Connect(IDC_LIST,wxEVT_COMMAND_LIST_ITEM_SELECTED,(wxObjectEventFunction)&CDlgCash::OnNMClickList);
    Connect(IDC_LIST,wxEVT_COMMAND_LIST_ITEM_ACTIVATED,(wxObjectEventFunction)&CDlgCash::OnNMDblclkList);
    wxLogDebug(_T("CreateCtrl"));
}

void CDlgCash::EnableAllCtrl( bool isEnable )
{
    if (m_addormod_==ADDMODEL) {
        FindWindowById(IDC_THEDAY)->Enable(isEnable);
        FindWindowById(IDC_COMBO_TIME)->Enable(isEnable);
        FindWindowById(IDC_EDIT_BALANCE)->Enable(isEnable);
        FindWindowById(IDC_EDIT_USER)->Enable(isEnable);
        FindWindowById(IDC_COMBO_TYPE)->Enable(isEnable);
        FindWindowById(IDC_RICHEDIT_REMARK)->Enable(isEnable);
        FindWindowById(IDC_LIST)->Enable(false);
        FindWindowById(IDC_BTN_SEARCH)->Enable(false);
        FindWindowById(IDC_EDIT_SEARCH)->Enable(false);
        FindWindowById(IDC_COMBO_SEARCH)->Enable(false);
        FindWindowById(IDC_BTN_DEL)->Enable(false);
    } else if (m_addormod_==MODMODEL) {
        FindWindowById(IDC_THEDAY)->Enable(false);
        FindWindowById(IDC_COMBO_TIME)->Enable(false);
        FindWindowById(IDC_EDIT_BALANCE)->Enable(isEnable);
        FindWindowById(IDC_EDIT_USER)->Enable(isEnable);
        FindWindowById(IDC_COMBO_TYPE)->Enable(isEnable);
        FindWindowById(IDC_RICHEDIT_REMARK)->Enable(isEnable);
        FindWindowById(IDC_LIST)->Enable(false);
        FindWindowById(IDC_BTN_SEARCH)->Enable(false);
        FindWindowById(IDC_EDIT_SEARCH)->Enable(false);
        FindWindowById(IDC_COMBO_SEARCH)->Enable(false);
        FindWindowById(IDC_BTN_DEL)->Enable(false);
    } else {
        FindWindowById(IDC_THEDAY)->Enable(isEnable);
        FindWindowById(IDC_COMBO_TIME)->Enable(isEnable);
        FindWindowById(IDC_EDIT_BALANCE)->Enable(isEnable);
        FindWindowById(IDC_EDIT_USER)->Enable(isEnable);
        FindWindowById(IDC_COMBO_TYPE)->Enable(isEnable);
        FindWindowById(IDC_RICHEDIT_REMARK)->Enable(isEnable);
        FindWindowById(IDC_LIST)->Enable(true);
        FindWindowById(IDC_BTN_SEARCH)->Enable(true);
        FindWindowById(IDC_EDIT_SEARCH)->Enable(true);
        FindWindowById(IDC_COMBO_SEARCH)->Enable(true);
        FindWindowById(IDC_BTN_DEL)->Enable(true);
    }
    wxLogDebug(_T("EnableAllCtrl"));
}

void CDlgCash::InitComboBoxAndListCtrl()
{
    wxChoice *pchoice=wxDynamicCast(FindWindowById(IDC_COMBO_SEARCH),wxChoice);
    wxListCtrl *pList=wxDynamicCast(FindWindowById(IDC_LIST),wxListCtrl);
    pList->SetSingleStyle(wxLC_REPORT);
    pList->SetSingleStyle(wxLC_SINGLE_SEL);
    const vector<pair<wxString,wxString> >&chs_fileds_name=GetFiled_CHS_Name(_T("name_VcashFull"));
    auto it=chs_fileds_name.cbegin();
    for (; it!=chs_fileds_name.cend(); ++it) {
        wxLogDebug(_T("%s "),it->first);
        pchoice->Append(it->first);
        pList->InsertColumn(pList->GetColumnCount(),it->first,wxLIST_FORMAT_LEFT,wxLIST_AUTOSIZE);
    }
    pchoice=wxDynamicCast(FindWindowById(IDC_COMBO_TIME),wxChoice);
    pchoice->Append(MORNING);
    pchoice->Append(AFTERNOON);
    pchoice->Append(NIGHT);
    pchoice->Append(TIME_OTHER);

    pchoice=wxDynamicCast(FindWindowById(IDC_COMBO_TYPE),wxChoice);
    wxString sql=_T("where typeid like '1%'");
    vector<pair<wxString,int> > typenameandid_=GetTypeName_ID(sql);
    vector<pair<wxString,int> >::const_iterator nameidit=typenameandid_.begin();
    for (; nameidit!=typenameandid_.end(); ++nameidit) {
        pchoice->Append(nameidit->first);
    }
    wxLogDebug(_T("InitComboBoxAndListCtrl"));
}


void CDlgCash::OnBnClickedBtnSearch(wxCommandEvent& event)
{
    wxBusyCursor waticursor;
    wxChoice *pBox=wxDynamicCast(FindWindowById(IDC_COMBO_SEARCH),wxChoice);
    int index=pBox->GetSelection();
    wxString strComboTextFirst,strComboTextSecond;
    if (index != wxNOT_FOUND) {
        strComboTextFirst=pBox->GetString(index);
    }
    wxListCtrl *pList=wxDynamicCast(FindWindowById(IDC_LIST),wxListCtrl);
    const vector<pair<wxString,wxString> >&chs_filed_name=GetFiled_CHS_Name(_T("name_VcashFull"));
    if (chs_filed_name.empty()) {
        return;
    }
    vector<pair<wxString,int> >fileds;
    map<wxString,wxString> condition;
    map<wxString,wxString>*pcondit=NULL;
    auto it=chs_filed_name.cbegin();
    for (; it!=chs_filed_name.cend(); ++it) {
        if (strComboTextFirst==it->first) {
            strComboTextSecond=it->second;
        }
        fileds.push_back(std::make_pair(it->second,(int)FILED_TEXT));
    }
    wxTextCtrl *pText=wxDynamicCast(FindWindowById(IDC_EDIT_SEARCH),wxTextCtrl);
    wxString csEditText=pText->GetValue();
    csEditText.Trim(true);
    csEditText.Trim(false);
    if (!csEditText.IsEmpty() && !strComboTextFirst.IsEmpty()) {
        condition[strComboTextSecond]=csEditText;
        pcondit=&condition;
    }
    SelFromDb(fileds,VIEWNAME,pcondit,_T("order by theday desc"),pList);
    pList->SetColumnWidth(0,wxLIST_AUTOSIZE);
    wxLogDebug(_T("OnBnClickedBtnSearch"));
}
void CDlgCash::OnBnClickedBtnDel(wxCommandEvent& event)
{
    wxListCtrl *pList=wxDynamicCast(FindWindowById(IDC_LIST),wxListCtrl);
    long selitemIndex=-1;
    selitemIndex=pList->GetNextItem(selitemIndex,wxLIST_NEXT_ALL,wxLIST_STATE_SELECTED);//得到选中的行数
    if (selitemIndex ==-1) {
        return;
    }
    wxString tip=_T("确实要删除[")+m_keyvalue_+_T("]的现金流记录吗？");
    if(wxNO==wxMessageBox(tip,_T("删除确认"),wxYES_NO)) {
        return;
    }
    map<wxString,wxString> condition;
    condition[_T("cashID")]=m_keyvalue_;
    if(DelFromDb(TABLENAME,&condition)) {
        wxMessageBox(_T("删除完成"),_T("删除完成"),wxOK);
        pList->DeleteItem(selitemIndex);
    } else {
        wxMessageBox(sqlite3_errmsg(g_db),_T("删除出错"));
        return;
    }
    wxLogDebug(_T("OnBnClickedBtnDel"));
}
void CDlgCash::OnBnClickedBtnExit(wxCommandEvent& event)
{
    if (ADDMODEL==m_addormod_ || MODMODEL==m_addormod_) {
        m_addormod_=-1;
        EnableAllCtrl(false);
        wxButton *pbtn=wxDynamicCast(FindWindowById(IDC_BTN_ADD_MOD),wxButton);
        pbtn->SetLabel(_T("添加"));
        pbtn=wxDynamicCast(FindWindowById(IDC_BTN_EXIT),wxButton);
        pbtn->SetLabel(_T("退出"));
        SetLabel(WINDOWS_TEXT);
    } else {
        Close();
    }
    wxLogDebug(_T("OnBnClickedBtnExit"));
}
void CDlgCash::OnBnClickedBtnAddMod(wxCommandEvent& event)
{
    wxString sql;
    if(ADDMODEL ==m_addormod_ || MODMODEL ==m_addormod_) {
        wxTextCtrl *pText=wxDynamicCast(FindWindowById(IDC_EDIT_BALANCE),wxTextCtrl);
        wxString csTypeName=pText->GetValue();
        if(csTypeName.IsEmpty()) {
            wxMessageBox(_T("收支数不能为空"),_T("错误"),wxOK);
            return;
        }
        wxChoice *pBox=wxDynamicCast(FindWindowById(IDC_COMBO_TIME),wxChoice);
        csTypeName=pBox->GetStringSelection();
        if(csTypeName.IsEmpty()) {
            wxMessageBox(_T("时间段不能为空"),_T("错误"),wxOK);
            return;
        }
        int nIdx;//行索引
        wxDatePickerCtrl *pDT=wxDynamicCast(FindWindowById(IDC_THEDAY),wxDatePickerCtrl);
        wxDateTime time_birthday=pDT->GetValue();
        wxString cashID,username,userID,typeName,strremark;
        double dBalance;
        int ntimeslot,ntypeID;
        pText=wxDynamicCast(FindWindowById(IDC_EDIT_BALANCE),wxTextCtrl);
        pText->GetValue().ToDouble(&dBalance);
        pText=wxDynamicCast(FindWindowById(IDC_EDIT_USER),wxTextCtrl);
        username=pText->GetValue();
        pBox=wxDynamicCast(FindWindowById(IDC_COMBO_TYPE),wxChoice);
        typeName=pBox->GetStringSelection();
        pText=wxDynamicCast(FindWindowById(IDC_RICHEDIT_REMARK),wxTextCtrl);
        strremark=pText->GetValue();
        ///////////////////////////////////////////////
        if (csTypeName.CmpNoCase(MORNING)==0) {
            ntimeslot=1;
        } else if (csTypeName.CmpNoCase(AFTERNOON)==0) {
            ntimeslot=2;
        } else if(csTypeName.CmpNoCase(NIGHT)==0) {
            ntimeslot=3;
        } else {
            ntimeslot=4;
        }
        ////由用户名称查找用户ID
        bool isfinduserid=false;
        sql=_T(" where username='")+username+_T("'");
        auto username_id=GetUserName_ID(sql);
        for (auto it=username_id.cbegin(); it!=username_id.cend(); ++it) {
            if (it->first==username) {
                userID=it->second;
                isfinduserid=true;
                break;
            }
        }
        if (isfinduserid==false) {
            wxLogDebug(_T("%s"),sql);
            wxMessageBox(_T("用户名有误,请重新输入"),_T("由用户名称查找用户ID出错"),wxOK);
            return;
        }
        ////由类别名称查找类别ID
        bool isfindtypeID=false;
        sql=_T(" where typename='")+typeName+_T("'");
        auto typename_id=GetTypeName_ID(sql);
        for (auto it=typename_id.cbegin(); it!=typename_id.cend(); ++it) {
            if (it->first==typeName) {
                ntypeID=it->second;
                isfindtypeID=true;
                break;
            }
        }
        if (isfindtypeID==false) {
            wxMessageBox(_T("类别名称有误，请重新选择"),_T("由类别名称查找类别ID出错"),wxOK);
            return;
        }
        wxListCtrl* pList=wxDynamicCast(FindWindowById(IDC_LIST),wxListCtrl);
        if (ADDMODEL==m_addormod_) { //由添加转为完成
            //添加新记录
            wxString sql=_T("select max(cashID) from ")+TABLENAME;
            sql+=_T(" where cashID like '")+time_birthday.Format(_T("%Y%m%d"))+_T("%'");
            sqlite3_stmt *stmt=NULL;
            if(sqlite3_prepare_v2(g_db,ToDefaultConv(sql),LenDefaultConv(sql),&stmt,0)==SQLITE_OK) {
                if(sqlite3_step(stmt)==SQLITE_ROW) {
                    if(sqlite3_column_text(stmt,0)!=NULL) {
                        wxString maxid=wxString((const char*)sqlite3_column_text(stmt,0),DefaultConv);
                        cashID=maxid.substr(8,maxid.length());//得到最后四位序号
                        long nid;
                        cashID.ToLong(&nid);
                        ++nid;
                        cashID=wxString::Format(_T("%s%04d"),time_birthday.Format(_T("%Y%m%d")),nid);
                    } else { //当天第一条记录
                        cashID=time_birthday.Format(_T("%Y%m%d"))+_T("0001");
                    }
                }
                if (sqlite3_finalize(stmt) != SQLITE_OK) {
                    wxLogDebug(_T("Error on sqlite3_finalize: %s"), sqlite3_errmsg(g_db));
                }
            } else {
                wxLogDebug(_T("%s 语法有误. 错误信息:%s"), sql,sqlite3_errmsg(g_db));
                if (sqlite3_finalize(stmt) != SQLITE_OK) {
                    wxLogDebug(_T("Error on sqlite3_finalize: %s"), sqlite3_errmsg(g_db));
                }
            }
            map<wxString,wxString> fileds;
            fileds[_T("cashID")]=cashID;
            fileds[_T("theTime")]=wxString::Format(_T("%d"),ntimeslot);
            fileds[_T("balance")]=wxString::Format(_T("%f"),dBalance);
            fileds[_T("userID")]=userID;
            fileds[_T("typeID")]=wxString::Format(_T("%d"),ntypeID);
            fileds[_T("remark")]=strremark;
            fileds[_T("theDay")]=time_birthday.FormatISODate();
            if(!AddIntoDb(fileds,TABLENAME)) {
                wxMessageBox(sqlite3_errmsg(g_db),_T("添加出错"));
                return;
            }
            m_addormod_=-1;
            EnableAllCtrl(false);
            wxButton *pbtn=wxDynamicCast(FindWindowById(IDC_BTN_ADD_MOD),wxButton);
            pbtn->SetLabel(_T("添加"));
            pbtn=wxDynamicCast(FindWindowById(IDC_BTN_EXIT),wxButton);
            pbtn->SetLabel(_T("退出"));
            SetLabel(WINDOWS_TEXT);

            //成功把该行插入到list控件中
            pList->InsertItem(pList->GetItemCount(),_T(""));
            nIdx=pList->GetItemCount()-1;
        } else if (MODMODEL==m_addormod_) { //由修改转为完成
            cashID=m_keyvalue_;
            map<wxString,wxString> fileds,condition;
            fileds[_T("theTime")]=wxString::Format(_T("%d"),ntimeslot);
            fileds[_T("balance")]=wxString::Format(_T("%f"),dBalance);
            fileds[_T("userID")]=userID;
            fileds[_T("typeID")]=wxString::Format(_T("%d"),ntypeID);
            fileds[_T("remark")]=strremark;
            fileds[_T("theDay")]=time_birthday.FormatISODate();
            condition[_T("cashID")]=cashID;
            if(!ModToDb(fileds,TABLENAME,&condition)) {
                wxMessageBox(sqlite3_errmsg(g_db),_T("修改出错"));
                return;
            }
            m_addormod_=-1;
            EnableAllCtrl(false);
            wxButton *pbtn=wxDynamicCast(FindWindowById(IDC_BTN_ADD_MOD),wxButton);
            pbtn->SetLabel(_T("添加"));
            pbtn=wxDynamicCast(FindWindowById(IDC_BTN_EXIT),wxButton);
            pbtn->SetLabel(_T("退出"));
            SetLabel(WINDOWS_TEXT);
            //成功把修改结果反应到list控件中
            nIdx=-1;
            nIdx = pList->GetNextItem(nIdx,wxLIST_NEXT_ALL,wxLIST_STATE_SELECTED);
            if(nIdx == -1) {
                return;
            }
        }
        pList->SetItem(nIdx,0,cashID);
        pList->SetItem(nIdx,1,time_birthday.FormatISODate());
        pList->SetItem(nIdx,2,wxString::Format(_T("%d"),ntimeslot));
        pList->SetItem(nIdx,3,wxString::Format(_T("%f"),dBalance));
        pList->SetItem(nIdx,4,username);
        pList->SetItem(nIdx,5,typeName);
        pList->SetItem(nIdx,6,strremark);
    } else {
        m_addormod_=ADDMODEL;
        EnableAllCtrl(true);
        wxButton *pbtn=wxDynamicCast(FindWindowById(IDC_BTN_ADD_MOD),wxButton);
        pbtn->SetLabel(_T("完成"));
        pbtn=wxDynamicCast(FindWindowById(IDC_BTN_EXIT),wxButton);
        pbtn->SetLabel(_T("取消添加"));
        SetLabel(WINDOWS_TEXT+_T("_添加"));
        wxTextCtrl *pText=wxDynamicCast(FindWindowById(IDC_EDIT_BALANCE),wxTextCtrl);
        pText->SetLabel(_T(""));
        pText=wxDynamicCast(FindWindowById(IDC_EDIT_USER),wxTextCtrl);
        pText->SetLabel(g_username);
    }
    wxLogDebug(_T("OnBnClickedBtnAddMod"));
}
void CDlgCash::OnNMClickList(wxListEvent& event)
{
    if(event.GetIndex()==-1) {
        return;
    }
    wxListCtrl *pList=wxDynamicCast(FindWindowById(IDC_LIST),wxListCtrl);
    m_keyvalue_=pList->GetItemText(event.GetIndex(),0);//得到主键值
    wxDateTime bir;
    if(bir.ParseDate(pList->GetItemText(event.GetIndex(),1))) {
        wxDatePickerCtrl *pDT=wxDynamicCast(FindWindowById(IDC_THEDAY),wxDatePickerCtrl);
        pDT->SetValue(bir);
    }
    long ntimeslot;
    pList->GetItemText(event.GetIndex(),2).ToLong(&ntimeslot);
    wxChoice *pBox=wxDynamicCast(FindWindowById(IDC_COMBO_TIME),wxChoice);
    pBox->SetSelection(ntimeslot-1);
    wxTextCtrl* pText=wxDynamicCast(FindWindowById(IDC_EDIT_BALANCE),wxTextCtrl);

    pText->SetValue(pList->GetItemText(event.GetIndex(),3));
    pText=wxDynamicCast(FindWindowById(IDC_EDIT_USER),wxTextCtrl);
    pText->SetValue(pList->GetItemText(event.GetIndex(),4));
    /////////////////////////////////////////////////////////////
    wxString strTmp=pList->GetItemText(event.GetIndex(),5);
    pBox=wxDynamicCast(FindWindowById(IDC_COMBO_TYPE),wxChoice);
    pBox->SetStringSelection(strTmp);
////////////////////////////////////////////////////////////////
    pText=wxDynamicCast(FindWindowById(IDC_RICHEDIT_REMARK),wxTextCtrl);
    pText->SetValue(pList->GetItemText(event.GetIndex(),6));
    wxLogDebug(_T("OnNMClickList"));
}
void CDlgCash::OnNMDblclkList(wxListEvent& event)
{
    if(event.GetIndex()==-1) {
        return;
    }
    wxListCtrl *pList=wxDynamicCast(FindWindowById(IDC_LIST),wxListCtrl);
    m_keyvalue_=pList->GetItemText(event.GetIndex(),0);//得到主键值
    m_addormod_=MODMODEL;
    EnableAllCtrl(true);
    wxButton *pbtn=wxDynamicCast(FindWindowById(IDC_BTN_ADD_MOD),wxButton);
    pbtn->SetLabel(_T("保存"));//修改完回到待增加状态
    pbtn=wxDynamicCast(FindWindowById(IDC_BTN_EXIT),wxButton);
    pbtn->SetLabel(_T("取消修改"));
    wxString text=WINDOWS_TEXT+_T("_修改");
    SetLabel(text);
    wxLogDebug(_T("OnNMDblclkList"));
}

